﻿(function ($) {
	var defaultOptions = {
		content: '#main-content',
		bottomBlock: '.next-content-bottom',
		innerToc: '.inner-toc',

		current: null,
		deleteUrl: null,
		publishStatusChangeUrl: null,
	};

	function init(options) {
		var opt = $.extend({}, defaultOptions, options);

		var $newToc = $('<a class="margin-left20">Новый заголовок</a>').data('after', '').on('click', null, opt, addInnerHead);

		var $editToc = $('<div class="right"/>').append($('<a class="mr10 edit">Edit</a>').on('click', null, opt, editInnerHead));
		$editToc.append($('<a class="mr10 delete">Delete</a>').on('click', null, opt, deleteInnerHead));

		var $innerToc = $(opt.innerToc, $(opt.content));

		if ($innerToc.length > 0) {
			$innerToc.each(function () {
				var $this = $(this);

				var $editBlock = $editToc.clone(true);
				$('.edit, .delete', $editBlock).data('id', $this.attr('id'));
				$this.append($editBlock);

				var $p = $newToc.clone(true).data('after', $('a', $this).attr('href')).wrap('<p />').parent();
				$this.after($p);
			});
		} else {
			$(opt.bottomBlock, $(opt.content)).filter(':first').before($newToc.wrap('<p/>').parent());
		}

		$('#published-block a, #unpublished-block a').on('click', null, opt, changePublishState);
	}

	function changePublishState(e) {
		e.preventDefault();

		if (confirm('Поменять статус?')) {
			$.ajax({
				type: 'PUT',
				url: e.data.publishStatusChangeUrl,
				data: { currentUrl: e.data.current },
				dataType: 'json',
				success: function () {
					window.location.reload(false);
				}
			});

		}
	}

	function editInnerHead(e) {
		e.preventDefault();

		var id = $(this).data('id');
		
		var $form = $('#new-part').clone(true).removeAttr('id').show();

		var $a = $('#' + id + ' a:first');
		$('[name="Text"]', $form).val($a.text());
		$('[name="Url"]', $form).val($a.parent().data('url'));
		var $id = $('[name="id"]', $form);
		if ($id.length == 0) {
			$id = $('<input type="hidden" />').attr('name', 'id');
			$form.append($id);
		}
		$id.val(id);
		
		$form.smarlyValidation({
			createBookUrl: $form.attr('action'),
			onSuccess: function () {
				window.location.reload(false);
			}
		});

		$.SmarlyModal.show({
			header: 'Редактирование',
			body: $form,
			closeOnApply: false,
			applyFunc: function () {
				var $current = $('[name="current"]', $form);
				if ($current.length == 0) {
					$form.append($('<input type="hidden" />').attr('name', 'current').val(e.data.current));
				}

				$form.submit();
			}
		});
	}

	function deleteInnerHead(e) {
		e.preventDefault();

		var id = $(this).data('id');
		
		$.ajax({
			type: 'DELETE',
			url: e.data.deleteUrl,
			data: { Id: id, currentUrl: e.data.current },
			dataType: 'json',
			success: function () {
				window.location.reload(false);
			}
		});

	}

	function addInnerHead(e) {
		e.preventDefault();
		var after = $(this).data('after');

		var $form = $('#new-part').clone(true).removeAttr('id').show();
		$form.smarlyValidation({
			createBookUrl: $form.attr('action'),
			onSuccess: function () {
				window.location.reload(false);
			}
		});

		$.SmarlyModal.show({
			header: 'Создание новой книги',
			body: $form,
			closeOnApply: false,
			applyFunc: function () {
				var $current = $('[name="current"]', $form);
				if ($current.length == 0) {
					$form.append($('<input type="hidden" />').attr('name', 'current').val(e.data.current));
				}

				var $after = $('[name="after"]', $form);
				if ($after.length == 0) {
					$form.append($('<input type="hidden" />').attr('name', 'after').val(after));
				}

				$form.submit();
			}
		});
	}

	$.extend({
		SmarlyAdminPage: init,
	});
})(jQuery);